/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mbeanserver;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

/**
 *
 * @author alan
 */
public final class MBServer {
    
    private static MBeanServer MBServer;
    private static JMXServiceURL JMXURL;
    private static JMXConnectorServer JMXConn;
    // private HtmlAdaptorServer JMXHTML;
    private static MBeanServerConnection JMXSC;
    
    private MBServer() throws RemoteException, IOException {
        try {
            LocateRegistry.createRegistry(17503);
        } catch (RemoteException e) {
            System.out.println("Não foi possível registrar a porta 17503: " + e.getMessage());
        }
        System.out.println("Inicializando MBean Server");
        this.MBServer = MBeanServerFactory.createMBeanServer("tcc");
    }
    
    public static void desconectMB() {
        try {
            JMXConn.stop();
        } catch (IOException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void ConnRMI() throws RemoteException, IOException {
        if (MBServer == null) {
            new MBServer();
        }
        try {
            System.out.println("Criando URL de conexão: service:jmx:rmi:///jndi/rmi://tcc:17503/server");
            System.out.println("POrta RMI registrada");
            JMXURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:17503/server");
            System.out.println("URL de conxão rmi criada com sucesso!");
            System.out.println("Criando conexão RMI...");
            JMXConn = JMXConnectorServerFactory.newJMXConnectorServer(JMXURL, null, MBServer);
            System.out.println("MBeans registrados");
            JMXConn.start();
            
            JMXSC = MBServer;
            if (System.getProperty("os.name").contains("Windows")) {
                ObjectName windows = new ObjectName("tcc:type=MACHINEWINDOWS");
                JMXSC.createMBean("mbeans.AgentWindows", windows);
            } else {
                if (System.getProperty("os.name").contains("Linux")) {
                    ObjectName linux = new ObjectName("tcc:type=MACHINELINUX");
                    JMXSC.createMBean("mbeans.AgentLinux", linux);
                    MBeanInfo as = JMXSC.getMBeanInfo(linux);
                    for (MBeanAttributeInfo intro : as.getAttributes()) {
                        System.out.println(intro.getName());
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstanceNotFoundException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IntrospectionException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ReflectionException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstanceAlreadyExistsException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MBeanException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotCompliantMBeanException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedObjectNameException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
